<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 11:04
 */
namespace app\index\controller;
use think\Request;
use think\Db;
use think\View;
use think\Loader;
use think\Session;
use think\Controller;

class User extends Controller
{

    //用户信息
    public function info()
    {
        $Login_userId =    Session::get('Login_userId');
        $user =  Db::name('member')->where(array('memberId'=>$Login_userId))->find();
        $headImgFilePath = filepath($user['headImgFileId']);
        if($headImgFilePath  !=null){
            $this->view->assign('headImgFilePath',$headImgFilePath);
        }
        $this->view->assign('user',$user);
        return view('userinfo');
    }
    //我的推荐推荐码
    public function mycommend()
    {
        $providerId =  getProviderId();
        $Login_userId=  Session::get('Login_userId');
        $where=array(
            'memberId' => $Login_userId,
        );
        $invitationCode =   Db::name('member')->where($where)->value('invitationCode');
        $memberName=   Db::name('member')->where($where)->value('memberName');
        $where2=array(
            'providerId' =>$providerId,
        );
        $providerLink =   Db::name('provider')->where($where2)->value('providerLink');
        $this->view->assign('memberName',$memberName);
        $this->view->assign('providerId',$providerId);
        $this->view->assign('providerLink', $providerLink);
        $this->view->assign('invitationCode', $invitationCode);
        return view('mycommend');
    }
    //收货地址
    public function address()
    {
        //获取登录人id
        $Login_userId=  Session::get('Login_userId');
        $where=array(
            'memberId' => $Login_userId,
            'isDefault' => ['=','0'],
        );

        $buyerAddress =   Db::name('buyerAddress')->where($where)->select();
        $where2=array(
            'memberId' => $Login_userId,
            'isDefault' => ['=','1'],
        );
        $isDefault =   Db::name('buyerAddress')->where($where2)->find();
        if($isDefault){
            $isDefault['province'] =  Db::name('area')->where(array('areaId'=> $isDefault['province']))->value('areaName');
            $isDefault['city'] = Db::name('area')->where(array('areaId'=> $isDefault['city']))->value('areaName');
            $isDefault['district'] =  Db::name('area')->where(array('areaId'=> $isDefault['district']))->value('areaName');
        }

        foreach( $buyerAddress as $k => $v){
            $provinceId  = $v['province'];
            $cityId  = $v['city'];
            $districtId  = $v['district'];
            $provinceName =    Db::name('area')->where(array('areaId'=>$provinceId))->value('areaName');
            $cityName =    Db::name('area')->where(array('areaId'=>$cityId))->value('areaName');
            $districtName =    Db::name('area')->where(array('areaId'=>$districtId))->value('areaName');
            $buyerAddress[$k]['province'] =  $provinceName;
            $buyerAddress[$k]['city'] =  $cityName;
            $buyerAddress[$k]['district'] =  $districtName;
        }

        $this->view->assign('isDefault', $isDefault);
        $this->view->assign('list', $buyerAddress);
        return view('buyeraddress');
    }
    //修改地址,添加地址
    public function editshippingaddress()
    {
        $address['memberId'] =  Session::get('Login_userId');

        if(Request::instance()->method() == 'POST') {
            $data = input();
            if($data['buyerAddressId'] ==''){
                $address['province'] = $data['provinceId'];
                $address['city'] = $data['cityId'];
                $address['district'] =  $data['districtId'];
                $address['address'] =  $data['address'];
                $address['linkman'] =  $data['linkman'];
                $address['tel'] =  $data['tel'];
                //数据是否已存在
                $buyerAddress =   Db::name('buyerAddress')->where($address)->find();
                if($buyerAddress != null){
                    $res['code'] = 'fail';$res['msg'] = '数据已存在';
                    return $res;
                }
                //查看是否有一条数据，没有的话默认为默认地址
                $Login_userId=  Session::get('Login_userId');
                $where=array(
                    'memberId' => $Login_userId,
                    'isDefault' => ['=','1'],
                );
                $buyerAddress =   Db::name('buyerAddress')->where($where)->find();
                if($buyerAddress == Null){
                    $isDefault = 1;
                }else{
                    $isDefault = 0;
                } //新加数据
                $address['buyerAddressId'] = autoId();
                $address['isDefault'] = $isDefault;

                $address['addDateTime'] = date('y-m-d h:i:s',time());
                //入库
                $result =   Db::name('buyerAddress')->insert($address);
                if($result){
                    $res['code'] = 'ok';$res['msg'] = '添加成功';
                }else{
                    $res['code'] = 'fail';$res['msg'] = '添加失败';
                }
                return  $res;
            }else{
                //修改数据
                $address['province'] = $data['provinceId'];
                $address['city'] = $data['cityId'];
                $address['district'] =  $data['districtId'];
                $address['address'] =  $data['address'];
                $address['linkman'] =  $data['linkman'];
                $address['tel'] =  $data['tel'];
                //数据是否已存在
                $buyerAddress =   Db::name('buyerAddress')->where($address)->find();
                if($buyerAddress != null){
                    //本条数据
                    if($buyerAddress['buyerAddressId'] == $data['buyerAddressId']){
                        $res['code'] = 'ok';$res['msg'] = '修改成功';
                        return $res;
                    }else{
                        $res['code'] = 'fail';$res['msg'] = '数据已存在';
                        return $res;
                    }
                }else{
                    $update =  Db::name('buyerAddress')->where(array('buyerAddressId'=>$data['buyerAddressId']))->update($address);
                    if($update){
                        $res['code'] = 'ok';$res['msg'] = '修改成功';
                    }else{
                        $res['code'] = 'fail';$res['msg'] = '修改失败';
                    }
                    return $res;
                }
            }
        }else{
            $shipaddressid = input('shipaddressid');
            if($shipaddressid  !=  null){
                //获取收货地址信息
                $address =   Db::name('buyerAddress')->where(array('buyerAddressId'=>$shipaddressid))->find();
                $this->view->assign('address', $address);
            }
            $group1 = Db::name('ServiceCategory')->where(array('parentId' => '0'))->select();
            $this->view->assign('group1', $group1);
            $oi =  Db::name('area')->where(array('parentId' => '0'))->select();
            $this->view->assign('oi', $oi);

            if($shipaddressid  !=  null){
                //获取所有省
                $p = Db::name('area')->where('parentId',0)->select();
                //获取当前省下地级市
                $d = Db::name('area')->where('parentId',$address['province'])->select();
                //获取当前地级市 的县
                $c = Db::name('area')->where('parentId',$address['city'])->select();
                $v = array(
                    'areaId'=>$address['city'],
                    'dId'   =>$address['district'],
                    'pId'   =>$address['province'],
                );
                $this->view->assign('city',$v);
            }else
            {
                //三级联动
                $area  = Session::get('Login_userId');
                //获取三级地区id
                $areaId = DB::name('member')->where('memberId',$area)->value('areaId');
                //获取地级市
                $dId = Db::name('area')->where('areaId',$areaId)->value('parentId');
                //获取省
                $pId = Db::name('area')->where('areaId',$dId)->value('parentId');
                //获取所有省
                $p = Db::name('area')->where('parentId',0)->select();
                //获取当前省下地级市
                $d = Db::name('area')->where('parentId',$pId)->select();
                //获取当前地级市 的县
                $c = Db::name('area')->where('parentId',$dId)->select();
                $v = array(
                    'areaId'=>$areaId,
                    'dId'   =>$dId,
                    'pId'   =>$pId,
                );
                $this->view->assign('city',$v);
            }


            $this->view->assign('p',$p);
            $this->view->assign('c',$c);
            $this->view->assign('d',$d);
            $pName=getAreaName(input('provinceId'));
            $cName=getAreaName(input('cityId'));
            $dName=getAreaName(input('districtId'));
            $this->view->assign('areaName',$pName.$cName.$dName);
            return view('addaddress');
        }

    }
    public  function selectarea(){
        $province=Db::name('area')->where('level',0)->select();
        $this->view->assign('province',$province);
        $city=Db::name('area')->where('level',1)->select();
        $this->view->assign('cit',$city);
        $area=Db::name('area')->where('level',2)->select();
        $this->view->assign('area',$area);
        return view('areaselect');
    }
    public  function setting(){
        if (!Session('Login_userId') && !Session('Login_userName')) {
            $this->redirect('/login');die;
        }
        $Login_userId=  Session::get('Login_userId');
        $where=array(
            'memberId' => $Login_userId,
        );
        $user =   Db::name('member')->where($where)->join('files','member.headImgFileId=files.filesId')->find();
        if(Request::instance()->method() == 'POST'){
            $data = $_POST;
            $sel =  $data ;
            $sel['memberId'] =  $Login_userId;
            $user =   Db::name('member')->where($sel)->find();
            if($user != null){
                $res['code'] = 'ok';$res['msg'] = '修改成功';
                return $res;
            }else{
                $result =   Db::name('member')->where($where)->update($data);
                if($result){
                    $res['code'] = 'ok';$res['msg'] = '修改成功';
                }else{
                    $res['code'] = 'fail';$res['msg'] = '修改失败';
                }
                return $res;
            }
        }else{
            //查询用户信息
            $user['phonefont'] = mobile()['phonefont'];
            $user['phoneback'] =  mobile()['phoneback'];
            $user['emailfontk']  =  mobile()['emailfontk'];
            //@出现位置
            //长度
            $user['emailback']  =  mobile()['emailback'];
            $this->view->assign('user', $user);
            return view('set');
        }
        return view('set');
    }
    public  function editUseName(){
        if(Request::instance()->method() == 'POST'){
            $Login_userId=  Session::get('Login_userId');
            $where=array(
                'memberId' => $Login_userId,
            );
            $data = $_POST;
            $sel =  $data ;
            $sel['memberId'] =  $Login_userId;
            $user =   Db::name('member')->where($sel)->find();
            if($user != null){
                $res['code'] = 'ok';$res['msg'] = '修改成功';
                return $res;
            }else{
                $result =   Db::name('member')->where($where)->update($data);
                if($result){
                    $res['code'] = 'ok';$res['msg'] = '修改成功';
                }else{
                    $res['code'] = 'fail';$res['msg'] = '修改失败';
                }
                return $res;
            }
        }else{
            return view('editusename');
        }

    }
    public function sendSms()
    {
        $Login_userId=  Session::get('Login_userId');
        if(!$Login_userId){
            $res['code'] = 'fail';
            $res['msg'] = '请先登录';
        }else{
            $where=array(
                'memberId' => $Login_userId,
            );
            $mobile =   Db::name('member')->where($where)->value('mobile');
            //发送验证码
            $sms_code = input('sms_code');
            $SmsSend = new \app\index\controller\SmsSend();
            $res =  $SmsSend -> index($sms_code,$mobile);
        }
        return $res;
    }
    public  function editEmail(){
        return view('editemail');
    }
//改绑手机号，改绑邮箱 发送邮件
    public function  sendEmail(){
        $title = input('title');
        $msg = input('msg');
        if(input('email')){
            $toemail = input('email');
        }else{
            $Login_userId=  Session::get('Login_userId');
            $where=array(
                'memberId' => $Login_userId,
            );
            $user =   Db::name('member')->where($where)->find();
            $toemail = $user['email'];
        }
        $emailCode = rand(1000,9999);
        $content = '验证码为 '.$emailCode.'，'.$msg;
        $res =  sendEmail($emailCode,$toemail,$title,$content);
        return $res;
    }
    public  function editPhone(){
        return view('editphone');
    }
}